﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LinQDemo.Models;
using LinQDemo.Models.SimpObject;
using LinQDemo.Models.MultiObject;

namespace LinQDemo.Controllers
{
    [HandleError]
    public class DemoController : Controller
    {
        StudentRepository studentRepository;
        ClassRepository classRepository;
        public DemoController()
        {
            studentRepository = new StudentRepository();
            classRepository = new ClassRepository();
        }

        public ViewResult ListStudents()
        {
            var allstudent = studentRepository.FindAllStudents();
            return View(allstudent.ToList());
        }

        public ViewResult ListStudentByClass()
        {
            string id = classRepository.FindFirst().ClassID;

            var allstudent = from i in studentRepository.FindAllStudents()
                                   where ( i.ClassID == id)
                                    select i;
            return View(allstudent.ToList());
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ViewResult ListStudentByClass(FormCollection formValues)
        {
            string id = formValues.Get("ClassID");
            var allstudent = from i in studentRepository.FindAllStudents()
                             where (i.ClassID == id)
                             select i;
            return View(allstudent.ToList());
        }

        public ViewResult ListStudentClassTeacher()
        {
            var studentclassteachers = from i in studentRepository.FindAllStudents()
                                   select new StudentClassTeacher
                                   { 
                                       StudentID = i.StudentID,
                                       StudentName = i.StudentName,
                                       ClassID = i.ClassID,
                                       TeacherID = i.Class.Teacher.TeacherID,
                                       TeacherName = i.Class.Teacher.TeacherName  
                                   };

            return View(studentclassteachers.ToList());
        }


        public ActionResult Edit(string id)
        {
            Student student = studentRepository.GetStudent(id);

            return View(student);
        } 
        [AcceptVerbs(HttpVerbs.Post)] 
        public ActionResult Edit(string id, FormCollection formValues) { 
         
            // Retrieve existing dinner 
            Student student = studentRepository.GetStudent(id);

            try
            {
                UpdateModel(student);
                studentRepository.Save();
                return RedirectToAction("ListStudents"); 
            }
            catch
            {
                foreach (var issue in student.GetRuleViolations())
                {
                    ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
                } 
                return View(student);
            } 
        }


        public ActionResult Add()
        {
            return View();
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Add(FormCollection formValues)
        {

            // Retrieve existing dinner 
            Student student = new Student();

            try
            {
                UpdateModel(student);
                studentRepository.Add(student);
                studentRepository.Save();
                return RedirectToAction("ListStudents");
            }
            catch
            {
                foreach (var issue in student.GetRuleViolations())
                {
                    ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
                }
                return View(student);
            }
        } 
    }
}
